home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / kriegspi / movecycl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-30  |  6.0 KB  |  270 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: movecycle.c,v 1.2 87/05/19 17:22:07 schoch Exp $";
  3. #endif
  4.  
  5. #include "externs.h"
  6.  
  7. #ifdef XKS
  8. movecycle()
  9. {
  10.     fd_set readfds, fds;
  11.     extern int sock;
  12.     int n;
  13.  
  14.     XSelectInput(window, ExposeWindow | ExposeRegion |
  15.     ButtonPressed | ButtonReleased | LeaveWindow | LeftDownMotion |
  16.     KeyPressed);
  17.     FD_ZERO(&fds);
  18.     FD_SET(dpyno(), &fds);
  19.     FD_SET(sock, &fds);
  20.     for(;;) {
  21.     while (XPending())
  22.         handle_input();
  23.     readfds = fds;
  24.     n = select(FD_SETSIZE, &readfds, 0, 0, 0);
  25.     if (n < 0) {
  26.         if (errno == EINTR)
  27.         continue;
  28.         perror("select");
  29.         exit(1);
  30.     }
  31.     if (n == 0)
  32.         exit(0);
  33.     if (FD_ISSET(dpyno(), &readfds))
  34.         handle_input();
  35.     if (FD_ISSET(sock, &readfds))
  36.         if (handle_sock()) {
  37.         FD_CLR(sock, &fds);
  38.         close(sock);
  39.         sock = -1;
  40.         }
  41.     }
  42.  
  43. }
  44.  
  45. extern bool note_on;
  46. int    pawntries=0;
  47. char    *colorname[3] = { "white", "black", "undecided" };
  48.  
  49. /* This function is called when either side tries a move.
  50.  * If the move is not valid, then we signal an error, no matter
  51.  * who tries the move (this is so we can see what our opponent is
  52.  * trying.
  53.  *     If the move is correct, then we toggle color.
  54.  */
  55. movetry(from, to, whose)
  56. u_char whose;
  57. {
  58.     /* We can cheat with this initialization  of pawntries because
  59.      * we know you can't take a pawn at the beginning of the games.
  60.      */
  61.     static pawnattempts=0;
  62.     static LIST checkdirs = NIL;
  63.     int l;
  64.     LIST check ();
  65.     char buf[128];
  66.  
  67.     mclear (CAPTURE);
  68.  
  69.     if ((l = legalmove (pawntries, &pawnattempts, checkdirs,
  70.                        from, to, color)) != TRUE
  71.         && !drawok [color] && !drawok [1 - color]
  72.         && !resign && !dead) {
  73.         illegal (l, color);
  74.         return l;
  75.     }
  76.     mclear (CHECK);
  77.     mclear(LEGAL);
  78.     if (!note_on) {
  79.         if (ourcolor == WHITE)
  80.             message("--- WHITE ---", MESSAGE);
  81.         else
  82.             message("--- BLACK ---", MESSAGE);
  83.     }
  84.  
  85.     if (!drawok [1 - color] && !drawok [color]
  86.         && !resign && !dead) {
  87.         makemove (from, to, color);
  88.         if (occupant [to] == KING && to == from + 2)
  89.             makemove (from + 3, from + 1, color);
  90.         if (occupant [to] == KING && to == from - 2)
  91.             makemove (from - 4, from - 1, color);
  92.         lastmovefrom = from;
  93.         lastmoveto = to;
  94.     }
  95.     if (!resign && drawok [1 - color] && !drawok [color]) {
  96.         drawok [1 - color] = FALSE;
  97.         message("Draw refused.", LEGAL);
  98.     }
  99.  
  100.     color = 1 - color;
  101.  
  102.     checkdirs = check (color);
  103.  
  104.     if (mate (pawnattempts, color)) {
  105.         if (checkdirs != NIL) {
  106.             message("CHECKMATE !", CHECK);
  107.             sprintf(buf, "%s wins.", colorname[1 - color]);
  108.             message(buf, TOMOVE);
  109.         } else {
  110.             message("STALEMATE", CHECK);
  111.             mclear(TOMOVE);
  112.         }
  113.         state = OVER;
  114.         mclear(PAWNTRIES);
  115.         return 0;
  116.     }
  117.     if (insufficient () || (drawok [WHITE] && drawok [BLACK])) {
  118.         message("Game ends in a draw.", CHECK);
  119.         mclear(TOMOVE);
  120.         mclear(PAWNTRIES);
  121.         state = OVER;
  122.         return 0;
  123.     }
  124.     if (resign) {
  125.         sprintf(buf, "%s resigns.", colorname[whose]);
  126.         message(buf, CHECK);
  127.         mclear(TOMOVE);
  128.         mclear(LEGAL);
  129.         mclear(PAWNTRIES);
  130.         state = OVER;
  131.         return 0;
  132.     }
  133.     if (dead) {
  134.         message("DEAD", CHECK);
  135.         message("lost your opponent (sorry)", MESSAGE);
  136.         state = OVER;
  137.         return 0;
  138.     }
  139.  
  140.     pawnattempts = 0;
  141.     pawntries = countpawntries (color);
  142.     if (drawok[1 - color]) {
  143.         message("Draw offered.", LEGAL);
  144.         if (color == ourcolor)
  145.             message("Type y or n.", TOMOVE);
  146.         else
  147.             mclear(TOMOVE);
  148.     } else {
  149.         sprintf(buf, "%s to move", colorname[color]);
  150.         message(buf, TOMOVE);
  151.     }
  152.  
  153.     if (option [ANNOUNCEPAWNS]) {
  154.         if(pawntries && pawnattempts < 3) {
  155.             if (pawntries == 1)
  156.                 strcpy(buf, "1 pawntry");
  157.             else
  158.                 sprintf(buf, "%d pawntries", pawntries);
  159.             message(buf, PAWNTRIES);
  160.         } else
  161.             mclear(PAWNTRIES);
  162.     } else
  163.         mclear(PAWNTRIES);
  164.  
  165.     reportchecks (checkdirs, kingloc [color]);
  166.     return 0;
  167. }
  168. #else XKS
  169. movecycle ()
  170. {
  171.     int from, to, pawntries, pawnattempts, l;
  172.     LIST check (), checkdirs;
  173.  
  174.     while (TRUE) {
  175.         mclear(CLOCK);
  176.         mclear(PROMPT);
  177.         pawnattempts = 0;
  178.         wprintw (win [TOMOVE], "%s to move\r", colorname [color]);
  179.         pawntries = countpawntries (color);
  180.         if (option [ANNOUNCEPAWNS] && pawntries && pawnattempts < 3)
  181.             if (pawntries == 1)
  182.                 wprintw (win [PAWNTRIES], "1 pawntry");
  183.             else
  184.                 wprintw (win [PAWNTRIES], "%d pawntries",
  185.                      pawntries);
  186.         checkdirs = check (color);
  187.         reportchecks (checkdirs, kingloc [color]);
  188.         if (mate (pawnattempts, color)) {
  189.             mclear(CHECK);
  190.             if (checkdirs != NIL)
  191.                 message("CHECKMATE !", CHECK);
  192.             else
  193.                 message("STALEMATE", CHECK);
  194.             state = OVER;
  195.             break;
  196.         }
  197.         if (insufficient () || (drawok [WHITE] && drawok [BLACK])) {
  198.             mclear(CHECK);
  199.             message("DRAW", CHECK);
  200.             state = OVER;
  201.             break;
  202.         }
  203.         if (resign) {
  204.             mclear(CHECK);
  205.             message("RESIGNS", CHECK);
  206.             state = OVER;
  207.             break;
  208.         }
  209.         if (dead) {
  210.             mclear(CHECK);
  211.             message("DEAD", CHECK);
  212.             mclear(MESSAGE);
  213.             state = OVER;
  214.             waddstr (win[MESSAGE], "lost your\nopponent\n(sorry)");
  215.         }
  216.         entermove (&from, &to, color, pawntries);
  217.         while ((l = legalmove (pawntries, &pawnattempts, checkdirs,
  218.                        from, to, color)) != TRUE
  219.         && !drawok [color] && !drawok [1 - color]
  220.         && !resign && !dead) {
  221.             illegal (l, color);
  222.             entermove (&from, &to, color, pawntries);
  223.         }
  224.         mclear(CAPTURE);
  225.         mclear(PAWNTRIES);
  226.         mclear(CHECK);
  227.         if (!drawok [1 - color] && !drawok [color]
  228.         && !resign && !dead) {
  229.             makemove (from, to, color);
  230.             if (occupant [to] == KING && to == from + 2)
  231.                 makemove (from + 3, from + 1, color);
  232.             if (occupant [to] == KING && to == from - 2)
  233.                 makemove (from - 4, from - 1, color);
  234.             lastmovefrom = from;
  235.             lastmoveto = to;
  236.         }
  237.         if (drawok [1 - color] && !drawok [color])
  238.             drawok [color] = FALSE;
  239.         color = 1 - color;
  240.         mclear(MESSAGE);
  241.     }
  242.     mclear(TOMOVE);
  243.     mclear(CLOCK);
  244.     mclear(CAPTURE);
  245.     mclear(PAWNTRIES);
  246.     while (TRUE) {
  247.         waddstr (win [PROMPT], "\rreview game?");
  248.         mclear(MESSAGE);
  249.         waddstr (win [MESSAGE], "type y or n");
  250.         mclear(INPUT);
  251.         waddstr (win [INPUT], ": ");
  252.         move (win [INPUT]->_cury + win [INPUT]->_begy,
  253.               win [INPUT]->_curx + win [INPUT]->_begx);
  254.         refresh ();
  255.         mclear(CHECK);
  256.         switch (getchar ()) {
  257.         case 'y':
  258.             mclear(PROMPT);
  259.             mclear(MESSAGE);
  260.             review();
  261.             break;
  262.         case 'n':
  263.             error ((char *) NULL);
  264.         default:
  265.             printf ("\007");
  266.         }
  267.     }
  268. }
  269. #endif XKS
  270.